﻿using System;
using System.Collections.Generic;
using Me.Prettyprint.Cassandra.Serializers;
using Me.Prettyprint.Cassandra.Service;
using Me.Prettyprint.Cassandra.Utils;
using Me.Prettyprint.Hector.Api;
using Me.Prettyprint.Hector.Api.Beans;
using Me.Prettyprint.Hector.Api.Factory;
using Me.Prettyprint.Hector.Api.Query;
using Org.Apache.Cassandra.Thrift;

namespace Me.Prettyprint.Cassandra.Model.Thrift
{
    /// <summary>
    /// A query for the thrift call get_slice
    /// </summary>
    /// <typeparam name="K"></typeparam>
    /// <typeparam name="N"></typeparam>
    public class ThriftSliceCounterQuery<K,N> : AbstractSliceQuery<K, N, long, CounterSlice<N>>, SliceCounterQuery<K,N>
    {
        public ThriftSliceCounterQuery(Keyspace k, Serializer<K> keySerializer,
            Serializer<N> nameSerializer) :
            base(k, keySerializer, nameSerializer, LongSerializer.Instance)
        {
        }
        public K Key
        {
            get;
            set;
        }
        public SliceCounterQuery<K, N> SetKey(K key)
        {
            Key = key;
            return this;
        }
        public override string ToString()
        {
            return "CounterSliceQuery(" + Key + "," + ToStringInternal() + ")";
        }
        public new SliceCounterQuery<K, N> SetColumnNames(List<N> columnNames) 
        {
            return (SliceCounterQuery<K, N>) base.SetColumnNames(columnNames);
        }
        public new SliceCounterQuery<K, N> SetRange(N start, N finish, bool reversed, int count)
        {
            return (SliceCounterQuery<K, N>) base.SetRange(start, finish, reversed, count);
        }
        public new SliceCounterQuery<K, N> SetColumnFamily(string cf)
        {
            return (SliceCounterQuery<K, N>) base.SetColumnFamily(cf);
        }
        public override QueryResult<CounterSlice<N>> Execute()
        {
            Func<KeyspaceService, CounterSlice<N>> x = (KeyspaceService ks) =>
            {
                ColumnParent columnParent = new ColumnParent(ColumnFamilyName);
                List<CounterColumn> thriftRet = ks.GetCounterSlice(KeySerializer.ToBytes(Key), columnParent, GetPredicate());
                return new CounterSliceImpl<N>(thriftRet, ColumnNameSerializer);
            };
            return new QueryResultImpl<CounterSlice<N>>(
                Keyspace.DoExecute(new KeyspaceOperationCallbackBuilder<CounterSlice<N>>(x)), this);
        }
    }
}
